/*
from:
   [{
        name: "可乐",
        categories: ["热门", "饮料"],
    },
    {
        name: "苹果",
        categories: ["热门", "食物"],
    },
    {
        name: "洗衣液",
        categories: ["生活用品"],
    },
];
to:
[
    { name: "热门", categories: ["可乐", "苹果"] },
    { name: "饮料", categories: ["可乐"] },
    { name: "食物", categories: ["苹果"] },
    { name: "生活用品", categories: ["洗衣液"] },
];
 */
const arr = [{
        name: "可乐",
        categories: ["热门", "饮料"],
    },
    {
        name: "苹果",
        categories: ["热门", "食物"],
    },
    {
        name: "洗衣液",
        categories: ["生活用品"],
    }]
function label(arr) {
    let resArr = []
    let map = new Map()
    arr.forEach(item => {
        item.categories.forEach(label => {
            if (!map.has(label)) {
                map.set(label,[item.name])
            }
            else {
                map.get(label).push(item.name)
            }
        })
    })
    map.forEach((key,value) => {
        resArr.push({name : value, categories : key})
    })
    return resArr
}

console.log(label(arr));


